﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 算法基本功.算法._0_数组._1_双指针.额外数学题
{
    internal class 螺旋矩阵2
    {
        //这个是给一个n 先生成矩阵 然后螺旋填充返回结果
        public int[][] GenerateMatrix(int n)
        {
            int[][] matrix = new int[n][];
            for (int i = 0; i < n; i++)
            {
                matrix[i] = new int[n];
            }

            int upper_bound = 0, lower_bound = n - 1;
            int left_bound = 0, right_bound = n - 1;
            int num = 1;

            while (num <= n * n)
            {
                if (upper_bound <= lower_bound)
                {
                    for (int j = left_bound; j <= right_bound; j++)
                    {
                        matrix[upper_bound][j] = num++;
                    }
                    upper_bound++;
                }

                if (left_bound <= right_bound)
                {
                    for (int i = upper_bound; i <= lower_bound; i++)
                    {
                        matrix[i][right_bound] = num++;
                    }
                    right_bound--;
                }

                if (upper_bound <= lower_bound)
                {
                    for (int j = right_bound; j >= left_bound; j--)
                    {
                        matrix[lower_bound][j] = num++;
                    }
                    lower_bound--;
                }

                if (left_bound <= right_bound)
                {
                    for (int i = lower_bound; i >= upper_bound; i--)
                    {
                        matrix[i][left_bound] = num++;
                    }
                    left_bound++;
                }
            }

            return matrix;
        }
    }
}
